package com.rockxin.cat.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.bean.UseBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import cn.qqtheme.framework.intefaces.IGetAddressPicker;
import cn.qqtheme.framework.picker.AddressPicker;
import cn.qqtheme.framework.task.AddressInitTask;
import rx.Subscriber;

/**
 * Created by cat on 18-3-3.
 */

public class AddBankCardActivity extends BaseActivity {
    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_banck_card_name)
    EditText etBanckCardName;
    @BindView(R.id.et_banck_card_num)
    EditText etBanckCardNum;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_bank_adrress)
    TextView tvBankAdrress;
    @BindView(R.id.et_banck_card_phone)
    EditText etBanckCardPhone;
    @BindView(R.id.btn_send_code)
    Button btnSendCode;
    @BindView(R.id.et_banck_card_code)
    EditText etBanckCardCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private Boolean isSendCode = false;

    private List<UseBean> bankList = new ArrayList<>();
    private String bankName;
    //银行id
    private String bankId = "-1";

    private String token;
    private String adrress;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        isSendCode = false;
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_add_bank_card;
    }

    @Override
    public void initData() {
        initView();
        setListener();
        getData();
    }

    public void initView() {
        title.setText("添加银行卡");
        etBanckCardName.setSingleLine();
        etBanckCardNum.setSingleLine();
        etBanckCardPhone.setSingleLine();
        etBanckCardCode.setSingleLine();

        Intent getIntent = getIntent();
        token = getIntent.getStringExtra("token");
    }


    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvBank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddBankCardActivity.this, BankListActivity.class);
                startActivity(intent);
            }
        });
        tvBankAdrress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AddressInitTask(AddBankCardActivity.this, new IGetAddressPicker() {
                    @Override
                    public void getAddress(AddressPicker.Province province, AddressPicker.City city, AddressPicker.County county) {
                        Logger.d(province.getAreaId() + "  " + city.getAreaId() + "  " + county.getAreaId());
                        Logger.d(province.getAreaName() + "  " + city.getAreaName() + "   " + county.getAreaName());
                        adrress = province.getAreaName() + " " + city.getAreaName() + " " + county.getAreaName();
                        tvBankAdrress.setText(province.getAreaName() + " " + city.getAreaName() + " " + county.getAreaName());
                    }
                }).execute("北京", "北京", "东城区");
            }
        });
        btnSendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMobileNO()) {
                    ToastUtils.showShortToastSafe("手机号不正确");
                    return;
                }
                if (!isSendCode) {
                    isSendCode = true;
                    timer.start();
                    final CatNetWork catNetWork = new CatNetWork();
                    catNetWork.sendBindcardCode(etBanckCardPhone.getText().toString(), token, new Subscriber<HttpResult>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult httpResult) {
                            Logger.json(new Gson().toJson(httpResult));
                            ToastUtils.showShortToastSafe(httpResult.getInfo());
                        }
                    });
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btnSendCode.setText((millisUntilFinished / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            isSendCode = false;
            btnSendCode.setText("获取验证码");
        }
    };

    private boolean isMobileNO() {
        final String phone = etBanckCardPhone.getText().toString();
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        boolean bool = !m.matches();
        return bool;
    }

    private void getData() {

    }

    private void submit() {
        final String name = etBanckCardName.getText().toString();
        final String banckCardNum = etBanckCardNum.getText().toString();
        final String bankAdrress = adrress;
        final String phone = etBanckCardPhone.getText().toString();
        final String code = etBanckCardCode.getText().toString();

        if (name.length() < 2) {
            ToastUtils.showShortToastSafe("持卡人姓名不正确");
            return;
        } else if (banckCardNum.length() < 1) {
            ToastUtils.showShortToastSafe("银行卡卡号不能为空");
            return;
        } else if (bankId.equals("-1")) {
            ToastUtils.showShortToastSafe("请选择银行");
            return;
        } else if (bankAdrress.length() < 1) {
            ToastUtils.showShortToastSafe("请选择银行地址");
            return;
        } else if (code.length() < 1) {
            ToastUtils.showShortToastSafe("验证码不能为空");
            return;
        }

        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.addBank(token, name, bankAdrress, bankId, banckCardNum, phone, code, new Subscriber<HttpResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                Logger.json(new Gson().toJson(httpResult));
                ToastUtils.showShortToastSafe(httpResult.getInfo());
//
//                Intent intent = new Intent(AddBankCardActivity.this, SetupPaymentPasswdActivity.class);
//                intent.putExtra("token", token);
//                startActivity(intent);
                finish();
            }
        });

    }

    @org.simple.eventbus.Subscriber(tag = "bank_list")
    public void getBankEvent(UseBean useBean) {
        Logger.d("getBankEvent=====" + useBean.getId() + "    " + useBean.getName());
        tvBank.setText(useBean.getName());
        bankId = useBean.getId();
    }
}
